﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestExtensions
{
    public abstract class QuickTestBase : IDisposable
    {
        #region Variables

        private bool _disposed;

        #endregion

        #region Constructors

        public QuickTestBase()
        {
            Repository = new TestRepository();
        }

        ~QuickTestBase()
        {
            Dispose(false);
        }

        #endregion

        #region Properties

        protected TestRepository Repository { get; private set; }

        #endregion

        #region Methods

        protected void RunTest(string testName)
        {
            using (var test = Repository.LoadTest(testName))
            {
                var results = test.Run();
                if (results.Failed)
                {
                    Assert.Fail(results.GetFailureMessage());
                }
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    Repository.Dispose();
                }

                _disposed = true;
            }
        }

        #endregion
    }
}